package network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoCilent {
    private Socket socket = null;
    public TcpEchoCilent(String serverIP,int serverPort) throws IOException {
        socket = new Socket(serverIP,serverPort);
    }

    public void start(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("客户端启动！");
        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()){
            while(true){
                System.out.println("->");
                String request = scanner.next();
           //发送请求给服务去
                PrintWriter writer = new PrintWriter(outputStream);
                writer.println(request);
                writer.flush();
                 Scanner scannerNetwork = new Scanner(inputStream);
                 String response =  scannerNetwork.next();
                System.out.println(response);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        TcpEchoCilent client = new TcpEchoCilent("127.0.0.1",9090);
        client.start();
    }
}
